﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DeepZoomPivotConstructor.VisualLib.Hotspots;

namespace DeepZoomPivotConstructor.VisualLib.Utilities
{
    public static class VisualHelpers
    {

        /// <summary>
        /// Creates a centering hot spot for the image passed in. Returns
        /// null if the input image is null. You can then add other things to
        /// thi s hot spot if you want later on.
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static VisualHotSpot CreateCenterHereHS(this Visual v)
        {
            if (v == null)
            {
                return null;
            }

            var bb = v.VisualBoundingBox;
            VisualHotSpot hs = new VisualHotSpot(bb.Width, bb.Height)
            {
                new HSActionCenterHere() { LeftClickToActivate = true}
            };

            hs.XPosition = bb.XPosition;
            hs.YPosition = bb.YPosition;
            hs.IncZIndex(v.ZIndex - 1);
            return hs;
        }

        /// <summary>
        /// Creates the visual hot spot for this visual, and returns it. No actions have been
        /// added at this point.
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static VisualHotSpot CreateVHS(this Visual v)
        {
            if (v == null)
            {
                return null;
            }

            var bb = v.VisualBoundingBox;
            VisualHotSpot hs = new VisualHotSpot(bb.Width, bb.Height);

            hs.XPosition = bb.XPosition;
            hs.YPosition = bb.YPosition;
            hs.IncZIndex(v.ZIndex - 1);
            return hs;
        }

        /// <summary>
        /// Creates a move-there hot spot. Returns null if the next guy has nothing in it.
        /// Other actions can be added to the hot spot if needed.
        /// </summary>
        /// <param name="v"></param>
        /// <param name="vDotNext"></param>
        /// <param name="keyToActivate"></param>
        /// <returns></returns>
        public static VisualHotSpot CreateCenterThereHS(this Visual v, Visual vDotNext, HotSpotActionBase.ActivationKeys keyToActivate)
        {
            if (vDotNext == null)
            {
                return null;
            }

            var bb = v.VisualBoundingBox;
            VisualHotSpot next = new VisualHotSpot(bb.Width, bb.Height)
            {
                new HSActionCenterThere(vDotNext) {KeysToActivate=keyToActivate}
            };
            next.XPosition = bb.XPosition;
            next.YPosition = bb.YPosition;
            next.IncZIndex(v.ZIndex - 1);
            return next;
        }

        /// <summary>
        /// Creates a single action HS. and wraps it in a holder. Don't use this if you need to
        /// have more than one action assoicated with a single hot spot.
        /// </summary>
        /// <param name="v"></param>
        /// <returns></returns>
        public static Visual WrapInCenterHereHS(this Visual v)
        {
            VisualHolder h = new VisualHolder();
            h.AddImage(v);
            h.AddImage(v.CreateCenterHereHS());
            return h;
        }

        /// <summary>
        /// Given a list of pre-configured visuals, wrap
        /// them all up in a holder along with the central image,
        /// and return them.
        /// </summary>
        /// <param name="v"></param>
        /// <param name="stuffToBeIn"></param>
        /// <returns></returns>
        public static Visual WrapWithVisuals(this Visual v, List<Visual> stuffToBeIn)
        {
            if (stuffToBeIn.Count == 0)
            {
                return v;
            }

            VisualHolder holder = new VisualHolder();
            foreach (var s in stuffToBeIn)
            {
                if (s != null)
                {
                    holder.AddImage(s);
                }
            }
            holder.AddImage(v);

            return holder;
        }
    }
}
